package ruide_info_mvc.base.core.exception.customException;

import ruide_info_mvc.base.constant.ResponseConstant;
import ruide_info_mvc.base.core.ioc.*;
import org.springframework.context.MessageSource;

/**
 * 基础异常处理
 * @ClassName BaseException
 * @Author wangBo
 * @Date 2019/7/4 0004 下午 4:47
 * @Version 1.0
 */
public abstract class BaseException extends Exception {

    // 错误信息
    protected String errorMsg;

    // 错误值
    protected Object[] errorParams;

    // 国际化
    protected MessageSource messageSource;

    public BaseException(Object[] errorParams) {
        this.errorParams = errorParams;
    }

    public BaseException(String errorMsg,Object [] errorParams) {
        this.errorMsg = errorMsg;
        this.errorParams = errorParams;
    }

    /**
     * 功能描述: 是否需要 获取 配置文件的值
     * @Author wangBo
     * @Date 2019/7/4 0004 下午 4:53
     * @return boolean
     **/
    protected abstract boolean yesOrNoGoMessage();

    public BaseException(){
        messageSource = (MessageSource)CustomIoc.getApplicationContext().getBean(ResponseConstant.messageSourceName);
    }

    public BaseException(String errorMsg) {
        messageSource = (MessageSource)CustomIoc.getApplicationContext().getBean(ResponseConstant.messageSourceName);
        this.errorMsg = errorMsg;
    }

    public String getErrorMsg() {
        String message;
        if (yesOrNoGoMessage())
            message = messageSource.getMessage(errorMsg , null ,null);
        else
            message = errorMsg;
        return message;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
